package com.usoft;

import akka.dispatch.ExecutionContexts;
import akka.dispatch.Filter;
import akka.dispatch.Futures;
import akka.japi.Function;
import scala.concurrent.Await;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

import java.util.concurrent.Executors;

/**
 * Created by liyanxin on 2015/1/9.
 */
public class FutureDemo6 {

    public static void main(String args[]) throws Exception {
        final ExecutionContextExecutorService ec = ExecutionContexts.fromExecutorService(
                Executors.newCachedThreadPool());

        Future<Integer> future1 = Futures.successful(4);
        Future<Integer> successfulFilter = future1.filter(Filter.filterOf(
                new Function<Integer, Boolean>() {
                    public Boolean apply(Integer i) {
                        return i % 2 == 0;
                    }
                }), ec);

        //When filter fails, the returned Future will be failed with a scala.MatchError
        System.out.println(Await.result(successfulFilter, Duration.create(5, "s")));
    }
}
